package com.aaa.sbm.service.impl;

import com.aaa.sbm.common.ResultUtil;
import com.aaa.sbm.dao.ProductDao;
import com.aaa.sbm.dao.StockOutApprovalDao;
import com.aaa.sbm.dao.StockOutDao;
import com.aaa.sbm.dao.StockOutDetailDao;
import com.aaa.sbm.entity.*;
import com.aaa.sbm.service.StockOutService;
import com.aaa.sbm.util.Page;
import com.aaa.sbm.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class StockOutServiceImpl implements StockOutService {

    @Resource
    private StockOutDao stockOutDao;

    @Resource
    private StockOutDetailDao stockOutDetailDao;

    @Resource
    private StockOutApprovalDao stockOutApprovalDao;

    Integer id;
    @Override
    public int insertStockOut(StockOut stockOut) {
        int i = stockOutDao.insertStockOut(stockOut);
        id = stockOut.getId();
        return i;
    }

    @Override
    public Result insertStockOutDetailIn(StockOutDetail stockOutDetail) {
        // 获取当前日期
        Date currentDate = new Date();
        // 获取出库数量
        int num=stockOutDetail.getQuantity();
        // 获取商品单价
        Double price=stockOutDetail.getPrice();
        // 得出商品总价
        stockOutDetail.setAmount(num*price);

        StockOut stockOut = new StockOut();
        stockOut.setOutCode(null);
        stockOut.setTotalAmount(stockOutDetail.getAmount());
        stockOut.setCustomer(stockOutDetail.getCustomer());
        stockOut.setStatus(2);
        stockOut.setCreateTime(currentDate);
        stockOut.setCompleteTime(null);
        stockOut.setRemark(stockOutDetail.getRemark());
        this.insertStockOut(stockOut);
        stockOutDetail.setOutId(id);
        int i = stockOutDetailDao.insertStockOutDetailDao(stockOutDetail);
        return i>0?ResultUtil.success("添加成功") : ResultUtil.fail("添加失败");    }

    @Override
    public Result getStockList(Page<StockOutDetail> page) {
        System.out.println(page.getPageNo()+" ----- "+page.getPageSize());
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        List<StockOutDetail> list = stockOutDetailDao.selectList(page.getData());
        PageInfo<StockOutDetail> pageInfo = new PageInfo<>(list);
        return ResultUtil.success(pageInfo);
    }

    @Override
    public Result updateStockOut(StockOutDetail stockOutDetail) {
        StockOutApproval stockOutApproval = new StockOutApproval();
        stockOutApproval.setProductId(stockOutDetail.getProductId());
        stockOutApproval.setQuantity(stockOutDetail.getQuantity());
        stockOutApproval.setPrice(stockOutDetail.getPrice());
        stockOutApproval.setAmount(stockOutDetail.getAmount());
        stockOutApproval.setCustomer(stockOutDetail.getCustomer());
        stockOutApproval.setStatus(0);
        stockOutApproval.setCreateTime(new Date());
        stockOutApproval.setApprovalTime(null);
        stockOutApproval.setRemark(stockOutDetail.getRemark());
        int rows = stockOutApprovalDao.insertStockOutApproval(stockOutApproval);
        return rows > 0 ? ResultUtil.success("审核已提交") : ResultUtil.fail("提交失败");
    }

    @Override
    public Result deleteStockOut(Integer id) {
        int rows = stockOutDetailDao.deleteStockOutDetailDao(id);
        int rows1 = stockOutDao.deleteStockOut(id);
        int a=rows+rows1;
        return a > 0 ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败");
    }

    @Override
    public Result batchStockOut(List<Integer> outIds) {
        StockOut stockOut = new StockOut();
        int i = 0;
        for (Integer outId : outIds) {
            stockOut.setId(outId);
            stockOut.setStatus(0);
            int i1 = stockOutDao.updateStockOut(stockOut);
            i=i+i1;
        }
        return i > 0 ? ResultUtil.success("批量入库成功") : ResultUtil.fail("入库失败");
    }
}
